import GameConfig from 'GameConfig';

// 加分道具
cc.Class({
    extends: cc.Component,

    properties: {
        scoreValue: 0,
        scoreAudio: {
            type: cc.AudioClip,
            default: null,
        },
    },

    onLoad() {
        this.scorePool = new cc.NodePool('ScoreFx');
    },

    init(game) {
        this.game = game;
        this.posY = GameConfig.INIT_BARRIER_POSY;
    },

    // 碰撞检测
    onCollisionEnter(other,self){
        this.game.scorePropsPool.put(this.node);
        // 加分
        this.game.gainScore(this.scoreValue);
        // 加分音效
        cc.audioEngine.playEffect(this.scoreAudio, false);
        // 加分动画
        this.game.gainAnim(this.node.position, this.scoreValue);
    },

    _move(posX, posY) {
        this.node.setPosition(cc.v2(posX, posY));
    },

    update (dt) {
        if (GameConfig.GAME_START) {
            this.posY -= GameConfig.PROPS_PLAYER_MOVE_SPEED;
            this._move(this.node.position.x, this.posY);
            // 超出显示范围，则放回池中
            if (this.posY <= -GameConfig.INIT_BARRIER_POSY) {
                this.game.scorePropsPool.put(this.node);
            }
        }
    },
});
